import request from "@/request/request.ts";
import { pageParams, responseRole, responseUserList, userItem } from "@/api/permissions/userApi/interface.ts";

enum Enum {
  fetchUserList = "/admin/acl/user/",
  deleteUser = "/admin/acl/user/remove",
  addUser = "/admin/acl/user/save",
  updateUser = "/admin/acl/user/update",
  fetchRoles = "/admin/acl/user/toAssign",
  doAssignRole = "/admin/acl/user/doAssignRole",
  batchRemove = "/admin/acl/user/batchRemove"
}

/* 获取用户列表的接口 */
export const fetchUserListAPI = (pageParams: pageParams): Promise<responseUserList> => {
  return request({
    url: `${Enum.fetchUserList}/${pageParams.page}/${pageParams.limit}`,
    method: "GET",
    params:{
      username:pageParams.username
    }
  });
};
/* 删除用户的接口 */
export const deleteUserAPI = (id: number) => {
  return request({
    url: `${Enum.deleteUser}/${id}`,
    method: "DELETE",
  });
};
/* 批量删除用户的接口 */
export const batchRemoveAPI = (idList: number[]): Promise<any> => {
  return request({
    url: Enum.batchRemove,
    method: "DELETE",
    data: idList,
  });
};
/* 添加或者修改用户的接口 */
export const addOrUpdateUserAPI = (data: userItem): Promise<any> => {
  if (data.id) {
    return request({
      url: Enum.updateUser,
      method: "PUT",
      data,
    });
  } else {
    return request({
      url: Enum.addUser,
      method: "POST",
      data,
    });
  }
};

/*获取全部角色列表和当前用户所拥有的角色列表接口*/
export const fetchRoleListAPI = (id: number): Promise<responseRole> => {
  return request({
    url: `${Enum.fetchRoles}/${id}`,
    method: "GET",
  });
};

/* 用户分配角色的接口 */
export const doAssignRoleAPI = (data): Promise<any> => {
  return request({
    url: `${Enum.doAssignRole}`,
    method: "POST",
    data,
  });
};
